<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\Customertype as Customertypes;
/**
 * 客户分类接口
 */
class Customertype extends Common
{
    protected $noNeedRight = 'getDetail';

    /**
     * 获取客户等级列表
     * @ApiMethod (POST)
     */
    public function getlevel()
    {
        if ($this->request->isPost()) {
            $result = Customertypes::getLevel();
            $this->success('查询成功',$result,200);
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取客户分类
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $type_name 分类名称
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?config('app_limit'):$limit;

            $type_name = $this->request->post('type_name');
            $type = $this->request->post('type');
            $pid = $this->request->post('pid');
            $where = [];
            if(!empty($type_name)){
                $where['type_name'] = ['like','%'.$type_name.'%'];
            }
            if($pid){
                $where['pid'] = $pid;
            }
            $status = $this->request->post('status');
            if ($status==-1){
                $where['status'] = 0;
            }else if(!empty($status)){
                $where['status'] = $status;
            }

            $result = Customertypes::getList($where,$page,$limit,$type);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取客户分类详情
     * @ApiMethod (POST)
     * @param int $id 客户分类ID
     */
    public function getdetail()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $result = Customertypes::getDetail($id);
            $this->success('查询成功',$result,200);
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加客户分类
     * @ApiMethod (POST)
     * @param array $detail 客户分类数据
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $pid = html_entity_decode($this->request->post('pid'));
            $type_name = html_entity_decode($this->request->post('type_name'));
            $image = html_entity_decode($this->request->post('image'));
            $status = html_entity_decode($this->request->post('status'));
            $detail = [
                'pid'        => $pid,
                'type_name'  => $type_name,
                'type_image' => $image,
                'status'     => $status,
            ];
            $ret = Customertypes::addDetail($detail);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 更新客户分类
     * @ApiMethod (POST)
     * @param array $detail 客户分类数据
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $id = html_entity_decode($this->request->post('id'));
            $pid = html_entity_decode($this->request->post('pid'));
            $type_name = html_entity_decode($this->request->post('type_name'));
            $image = html_entity_decode($this->request->post('image'));
            $status = html_entity_decode($this->request->post('status'));
            $detail = [
                'id'         => $id,
                'pid'        => $pid,
                'type_name'  => $type_name,
                'type_image' => $image,
                'status'     => $status,
            ];
            $ret = Customertypes::editDetail($detail);
            if($ret['status']==200){
                $this->success('更新成功','',200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除客户分类
     * @ApiMethod (POST)
     * @param int $id 客户分类id
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $ret = Customertypes::delDetail($id);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

}
